<template>
    <div>
        <!-- <div class="rotate resize" v-dragger @mousedown="beginResize(4)"></div> -->
        <div class="border" v-dragger @mousedown="beginResize(5)"></div>
        <div class="scale-e resize" v-dragger @mousedown="beginResize(1)"></div>
        <div class="scale-w resize" v-dragger @mousedown="beginResize(3)"></div>
        <div class="scale-n resize" v-dragger @mousedown="beginResize(0)"></div>
        <div class="scale-s resize" v-dragger @mousedown="beginResize(2)"></div>
    </div>
</template>
<style lang="scss">
.border {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border: 1px dotted black;
    box-sizing: border-box;
}

.resize {
    background: white;
    width: 6px;
    height: 6px;
    position: absolute;
    border: 1px solid black
}

.scale-e {
    cursor: ew-resize;
    right: -5px;
    top: 50%;
    transform: translateY(-50%);
}

.scale-s {
    cursor: ns-resize;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
}

.scale-n {
    cursor: ns-resize;
    top: -3px;
    left: 50%;
    transform: translateX(-50%);
}

.scale-w {
    left: -3px;
    top: 50%;
    transform: translateY(-50%);
    cursor: ew-resize;
}

.rotate {
    top: -10px;
    left: 50%;
}
</style>
<script>
import dragEventBus from '@/components/public/DraggableDirective'

let tmpParam
export default {
  props: [
    'item'
  ],
  methods: {
    beginResize: function (type) {
      tmpParam = {
        x: this.item.style.left ? this.item.style.left : this.$el.offsetLeft,
        y: this.item.style.top ? this.item.style.top : this.$el.offsetTop,
        w: this.item.style.width ? this.item.style.width : this.$el.offsetWidth,
        h: this.item.style.height ? this.item.style.height : this.$el.offsetWidth,
        r: this.item.style.transform ? this.item.style.transform : 0,
        type: type
      }
      console.log(tmpParam)
      dragEventBus.$on('dragger-offset', this.doResize)
      dragEventBus.$on('dragger-end', this.endResize)
    },
    doResize: function (e) {
      let oldX = parseFloat(/-?\d+/.exec(tmpParam.x)[0])
      let oldY = parseFloat(/-?\d+/.exec(tmpParam.y)[0])
      let oldW = parseFloat(/-?\d+/.exec(tmpParam.w)[0])
      let oldH = parseFloat(/-?\d+/.exec(tmpParam.h)[0])
      console.log(oldH, oldW, oldX, oldY)
      switch (tmpParam.type) {
        case 0: // n
          this.item.style.height = oldH - e.oY
          this.item.style.top = oldY + e.oY
          break
        case 1: // e
          console.log('east')
          this.item.style.width = oldW + e.oX
          break
        case 2: // s
          this.item.style.height = oldH + e.oY
          break
        case 3: // w
          console.log('west')
          this.item.style.width = oldW - e.oX
          this.item.style.left = oldX + e.oX
          break
        case 4: // rotate 好难做不来
                // console.log(Math.atan(e.oX / e.oY))
                // let arc = 0
                // if (e.oY === 1) arc = 90
                // arc = Math.atan(e.target.offsetLeft / e.oY + (oldH / 2))
                // this.item.style.transform = 'rotate(' + 180 / Math.atan(e.oX / e.oY) + 'deg)'
          break
        case 5:
          console.log('moveing')
          this.item.style.left = oldX + e.oX
          this.item.style.top = oldY + e.oY
          break
      }
    },
    endResize: function () {
      dragEventBus.$off('dragger-offset', this.doResize)
      dragEventBus.$off('dragger-end', this.endResize)
      tmpParam = undefined
    }
  }
}
</script>
